package com.styytech.dadabao.utils;

import org.json.JSONObject;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

public class DeviceUtil {
	private static final String LOGTAG = DeviceUtil.class.getSimpleName();
	private static TelephonyManager telMgr;
	private static WifiManager wifiMgr;

	public static String getDeviceId(Context paramContext) {
		try {
			initManager(paramContext);
			if (telMgr != null) {
				Log.i(LOGTAG, "telMgr is not null");
				String str5 = telMgr.getDeviceId();
				Log.i(LOGTAG, "imei is " + str5);
				if (!isInvalid(str5))
					return str5;
			}
			Log.i(LOGTAG, "telMgr is null");
			if (wifiMgr != null) {
				Log.i(LOGTAG, "wifiMgr is not null");
				WifiInfo localWifiInfo = wifiMgr.getConnectionInfo();
				Log.i(LOGTAG, "wifiInfo is " + localWifiInfo);
				if (localWifiInfo != null) {
					String str4 = localWifiInfo.getMacAddress();
					Log.i(LOGTAG, "macAddr is " + str4);
					if (!isInvalid(str4))
						return "MAC:" + str4;
				}
			}
			Log.i(LOGTAG, "wifiMgr is null");
			String str2 = telMgr.getSimSerialNumber();
			Log.i(LOGTAG, "simSN is " + str2);
			if (!isInvalid(str2)) {
				String str3 = "SIMSN:" + str2;
				return str3;
			}
		} catch (Exception localException) {
			localException.printStackTrace();
			String str1 = getUUID(paramContext);
			Log.i(LOGTAG, "uuid is " + str1);
			if (!isInvalid(str1))
				return "UUID:" + str1;
		}
		return null;
	}

	public static JSONObject getDeviceIds(Context paramContext) {
		JSONObject localJSONObject = new JSONObject();
		try {
			initManager(paramContext);
			if (telMgr != null) {
				localJSONObject.put("sn", telMgr.getSimSerialNumber());
				localJSONObject.put("imei", telMgr.getDeviceId());
				localJSONObject.put("subId", telMgr.getSubscriberId());
				localJSONObject
						.put("operName", telMgr.getNetworkOperatorName());
				localJSONObject.put("operId", telMgr.getNetworkOperator());
			}
			if (wifiMgr != null) {
				WifiInfo localWifiInfo = wifiMgr.getConnectionInfo();
				if (localWifiInfo != null) {
					String str = localWifiInfo.getMacAddress();
					if (!isInvalid(str))
						localJSONObject.put("mac", str);
				}
			}
			localJSONObject.put("model", Build.MODEL);
			localJSONObject.put("version", Build.VERSION.RELEASE);
			return localJSONObject;
		} catch (Exception localException) {
			localException.printStackTrace();
		}
		return localJSONObject;
	}

	private static String getUUID(Context paramContext) {
//		SYSharedPreferences localSYSharedPreferences = SYSharedPreferences
//				.getInstance();
		String str = null;
//		if (localSYSharedPreferences != null) {
//			str = localSYSharedPreferences.getString("UUID", null);
//			if (isInvalid(str)) {
//				str = UUID.randomUUID().toString();
//				localSYSharedPreferences.putString("UUID", str);
//			}
//		}
		return str;
	}

	private static void initManager(Context paramContext) {
		if (wifiMgr == null) {
			Object localObject2 = paramContext.getSystemService("wifi");
			if (localObject2 != null)
				wifiMgr = (WifiManager) localObject2;
		}
		if (telMgr == null) {
			Object localObject1 = paramContext.getSystemService("phone");
			if (localObject1 != null)
				telMgr = (TelephonyManager) localObject1;
		}
	}

	private static boolean isInvalid(String paramString) {
		return (paramString == null) || (paramString.trim().equals(""))
				|| (paramString.trim().length() < 5);
	}

	public static int px2dip(Context paramContext, float paramFloat) {
		return (int) (0.5F + paramFloat
				/ paramContext.getResources().getDisplayMetrics().density);
	}

	public static int px2sp(Context paramContext, float paramFloat) {
		return (int) (0.5F + paramFloat
				/ paramContext.getResources().getDisplayMetrics().scaledDensity);
	}
}
